<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17-4-25
 * Time: 下午5:02
 */

namespace Admin\Controller;

use Think\Controller;

class ProdcatesController extends Controller

{

    public function index()
    {
        $this->assign(array(
            'biaoti'=>'分类首页',
        ));

        $model=D('Prodcates');
        $datas=$model->catTree();
        $this->datas=$datas;

        $this->display();
    }

    public function add()
    {
        $model=D('Prodcates');
        $this->assign(array(
            'biaoti'=>'分类添加',
        ));
        if(IS_POST){
            //dump($_POST);die;
            if($model->create()){
                if($model->add()){
                    $this->success('添加成功',redirect('index'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error($model->getError());
            }
        }else{
            $datas=$model->catTree();
            $this->datas=$datas;
            $this->display();
        }
    }

    public function edit($id){

        $this->assign(array(
            'biaoti'=>'分类编辑页面',
        ));
        $model=D('Prodcates');
        if(IS_POST){
            if($model->create()){
                if($model->save() !==FALSE){
                    $this->success('修改成功',redirect('index'));
                }else{
                    $this->error('修改失败');
                }
            }else{
                $this->error($model->getError());
            }
        }else{
            $datas=$model->catTree();
            $this->datas=$datas;
            $row=$model->find($id);
            $this->assign('row',$row);
            $this->display();
        }





    }


    public function del($id){
        $model=D('Prodcates');
        $data=$model->where('pid='.$id)->select();
        //删除大分类后，属于其的小分类页被删除
        foreach ($data as $k=>$v){
            $model->delete($v['id']);
            //子分类两种需求，是全部变成0还是全部删除
//            $res['pid'] = 0;
//            $model->where('id='.$v['id'])->save($res);
        }
        $model->delete($id);
        $this->success('删除成功');
        }

} 